﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TVGuide.Common;
using TVGuide.DataAccess;
using TVGuide.DataAccess.Common;
using TVGuide.DataAccess.Repository;
using System.Text.RegularExpressions;
using TVGuide.Enums;

namespace TVGuide.Parser
{
    public class NTVPlusParseEngine
    {
        public static List<Program> ParseSpecificProgram(Channel channel, DateTime date)
        {
            List<Program> tvProgramList = new List<Program>();
            string URL = "http://www.ntvplus.ru/tv/schedule.xl?genre=3494&channel=" + channel.Id + "&date="+date.Day+"."+date.Month+"."+date.Year+"&time=day";
            string HTMLString = HTTPClient.GetSiteContent(URL, Encoding.UTF8, "UcomSite");

            string timePattern = "<div class=\"time\">.*?</div>";
            Regex rgx = new Regex(timePattern, RegexOptions.IgnoreCase);
            MatchCollection timesWithHTML = rgx.Matches(HTMLString);
            string progTitlePattern = "<div class=\\\"(content\\\">|currenttv\\\"></div>)<h5>.*?</h5>([^<].*?</div>)?";  
            Regex progTitleRgx = new Regex(progTitlePattern, RegexOptions.IgnoreCase);
            MatchCollection titlesWithHTML = progTitleRgx.Matches(HTMLString);

            if (timesWithHTML.Count == 0)
            {
                //TVGuide.Common.Utils.SendNotificationMail("Empty program list for " + channel.Name + " program parsed from NTVPLUS for the " + date.Date + " date.");
                return tvProgramList;
            }

            int hour;
            int minute;
            for (int i = 0; i < timesWithHTML.Count; i++)
            {
                Utils.seperateTimeProgramName(timesWithHTML[i].Value, out hour, out minute);

                Program tvProgram = new Program();
                if (hour > 5 && hour < 23)
                {

                    var origDate = new DateTime(
                        date.Year,
                        date.Month,
                        date.Day,
                        hour,
                        minute,
                        0
                        );

                    tvProgram.Name = Utils.removeHTMLTags(titlesWithHTML[i].Value);
                    tvProgram.Date = origDate.AddHours(1);
                    //tvProgram.Date = origDate;
                    tvProgram.ChannelId = channel.Id;
                    tvProgramList.Add(tvProgram);
                }
                else

                    if (hour == 23)
                    {
                        tvProgram.Name = Utils.removeHTMLTags(titlesWithHTML[i].Value);

                        var origDate = new DateTime(
                            date.Year,
                            date.Month,
                            date.AddDays(1).Day,
                            0,
                            minute,
                            0
                            );
                        tvProgram.Date = origDate;
                        tvProgram.ChannelId = channel.Id;
                        tvProgramList.Add(tvProgram);
                    }

                    else
                        if (hour < 6)
                        {
                            tvProgram.Name = Utils.removeHTMLTags(titlesWithHTML[i].Value);
                            var origDate = new DateTime(
                                date.Year,
                                date.Month,
                                date.AddDays(1).Day,
                                hour + 1,
                                minute,
                                0
                                );
                            tvProgram.Date = origDate;
                            tvProgram.ChannelId = channel.Id;
                            tvProgramList.Add(tvProgram);
                        }
            }


            return tvProgramList;
        }

        public static List<Program> ParseSpecificProgram(Channel channel)
        {
            return ParseSpecificProgram(channel, DateTime.Today);
        }

        public static List<Program> ParseAll()
        {
            return ParseAll(DateTime.Today);
        }

        public static List<Program> ParseAll(DateTime date)
        {
            //TODO Remove debud stuff
            DateTime start = DateTime.Now;
            List<Program> allNTVPlusPrograms = new List<Program>();

            foreach (Enums.NTVPlusChannels value in Enum.GetValues(typeof(Enums.NTVPlusChannels)))
            {
                Channel channel = new Channel();
                ChannelRepository chanRep = ChannelRepository.Instance();
                var id = (int)value;
                channel.Id = id;
                channel.Name = chanRep.Get(id).Name;
                if (chanRep.Get(id).Enabled > 0)
                    allNTVPlusPrograms.AddRange(ParseSpecificProgram(channel, date));

            }

            DateTime end = DateTime.Now;
            Console.WriteLine("Parsing for the date " + date.Date + "\nParse time is " + (end.Second - start.Second) + " second");
            return allNTVPlusPrograms;
        }
    }
}
